package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSON;
import com.woniuxy.entity.DTO.*;
import com.woniuxy.entity.Goods;
import com.woniuxy.entity.ShoppingCarts;
import com.woniuxy.mapper.OrdersMapper;
import com.woniuxy.mapper.ShoppingCartsMapper;
import com.woniuxy.producer.DirectProducer;
import com.woniuxy.service.ShoppingCartService;
import com.woniuxy.util.RabbitMQConst;
import com.woniuxy.util.UserContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private RedisTemplate redisTemplateInit;

    @Resource
    private ShoppingCartsMapper shoppingCartsMapper;
    @Resource
    private OrdersMapper  ordersMapper;

    @Resource
    private DirectProducer directProducer;

    @Override
    public Boolean buyAgain(String orderId) throws ParseException {
        OrderItemAndAddressDTO o = (OrderItemAndAddressDTO) redisTemplateInit.opsForHash().get("ORDERSERVICEIMPL:QUERYORDERITEMANDADDRESSBYORDERID", orderId + "");
        List<GoodsItemDTO> products =null;
        if (o == null){
             o = ordersMapper.queryOrderItemByOrderId(orderId);
             products = ordersMapper.queryOrderItemImage(orderId);
            o.setProducts( products);
        }else {
            products = o.getProducts();
        }

        for (GoodsItemDTO goodsItemDTO : products) {
            goodsItemDTO.setUserId(UserContext.getUserId());
        }
        String productsStr = JSON.toJSONString(products);
        directProducer.sendAddShoppingCart(productsStr, RabbitMQConst.ADD_SHOPPING_CART);
        return true;


    }

    @Override
    public Boolean addShoppingCartByProductId(ShoppingCarts shoppingCarts) throws ParseException {
        shoppingCarts.setUserId(UserContext.getUserId());
        directProducer.sendAddShoppingCartByProductId(JSON.toJSONString(shoppingCarts), RabbitMQConst.ADD_SHOPPING_CART_BY_PRODUCT_ID);
        return true;
    }

    /**
     * <p>用户端小程序购物车页面查询所有购物车信息。</p>
     *
     * @return 购物车商品列表
     */
    @Override
    public List<QueryShoppingCartGoodsDTO> queryShoppingCart() throws ParseException {
        //Integer userId = 1;
        Integer userId = UserContext.getUserId();
        return shoppingCartsMapper.selectShoppingCartByUserId(userId);
    }

    /**
     * <p>批量删除用户选中的购物车商品。</p>
     *
     * @param deleteBatchShoppingCartGoodsDTOs 用户选中的购物车商品
     */
    @Override
    public void deleteBatchShoppingCartGoods(List<DeleteBatchShoppingCartGoodsDTO> deleteBatchShoppingCartGoodsDTOs) throws ParseException {
        //Integer userId = 1;
        Integer userId = UserContext.getUserId();
        deleteBatchShoppingCartGoodsDTOs.forEach(p -> p.setUserId(userId));
        shoppingCartsMapper.deleteBatchShoppingCartGoods(deleteBatchShoppingCartGoodsDTOs);
    }

    /**
     * <p>添加普通商品到购物车</p>
     * @param goodsId
     */
    @Override
    public void addShoppingCartForCommonGoods(Integer goodsId) throws ParseException {
        Integer userId = UserContext.getUserId();
        // 判断该商品是否第一次被该用户添加
        Integer cartId = shoppingCartsMapper.selectCartIdByUserIdAndGoodsId(userId, goodsId);
        if (cartId == null) { // 第一次添加，插入数据
            shoppingCartsMapper.insertShoppingCartForCommonGoods(goodsId, userId);
        } else { // 第二次及之后添加，更新数据
            shoppingCartsMapper.updateShoppingCartForCommonGoods(cartId, goodsId, userId);
        }
    }

    /**
     * <p>查询用户默认收货地址</p>
     * @return
     */
    @Override
    public AddressDTO queryDefaultAddress() throws ParseException {
        return shoppingCartsMapper.selectDefaultAddressByUserId(UserContext.getUserId());
    }
}
